package com.treasure.mall.biz.service.core.contact.impl;

import com.alibaba.fastjson.JSONObject;
import com.treasure.mall.biz.base.AbstractSearchableCrudService;
import com.treasure.mall.biz.base.SpecificationAndSort;
import com.treasure.mall.biz.dto.BaseSaveDTO;
import com.treasure.mall.biz.dto.BaseSearchDTO;
import com.treasure.mall.biz.dto.common.ChatMessageBodyDTO;
import com.treasure.mall.biz.dto.condition.ContactHistoryMessageCondition;
import com.treasure.mall.biz.entity.contact.ContactHistoryMessagePO;
import com.treasure.mall.biz.entity.contact.ContactHistoryMessagePO_;
import com.treasure.mall.biz.entity.user.UserPO;
import com.treasure.mall.biz.entity.user.UserPO_;
import com.treasure.mall.biz.repository.contact.ContactHistoryMessageRepository;
import com.treasure.mall.biz.service.core.contact.ContactHistoryMessageService;
import com.treasure.mall.zbq.common.ienum.chat.ChatEnums;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * <p>
 * +---------------------------------------------------------
 * * @package: com.treasure.mall.biz.service.core.contact.impl
 * * @author: guomw (guomwchen@foxmail.com)
 * * @date: 2020/8/11 5:42 下午
 * * @since: V1.0.0
 * +---------------------------------------------------------
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 *
 */
@Service
public class ContactHistoryMessageServiceImpl extends AbstractSearchableCrudService<ContactHistoryMessagePO, Long> implements ContactHistoryMessageService {


    protected ContactHistoryMessageServiceImpl(ContactHistoryMessageRepository repository) {
        super(repository);
    }

    @Override
    protected SpecificationAndSort<ContactHistoryMessagePO> specificationAndSort(BaseSearchDTO searchDTO) {
        ContactHistoryMessageCondition condition = (ContactHistoryMessageCondition) searchDTO;
        Specification<ContactHistoryMessagePO> specification = (root, query, cb) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (condition.getMerchantId() != null) {
                predicates.add(cb.equal(root.get(ContactHistoryMessagePO_.merchantId), condition.getMerchantId()));
            }

            if (condition.getUserId() != null) {
                predicates.add(cb.equal(root.get(ContactHistoryMessagePO_.userPO).get(UserPO_.userId), condition.getUserId()));
            }

            return cb.and(predicates.toArray(new Predicate[predicates.size()]));
        };
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        return new SpecificationAndSort<>(specification, sort);
    }

    @Override
    public void save(UserPO user, Long merchantId, ChatMessageBodyDTO dto) {
        ContactHistoryMessagePO po = new ContactHistoryMessagePO();
        po.setGroupId(dto.getGroupId());
        po.setUserPO(user);
        po.setMerchantId(merchantId);
        po.setMessageBody(JSONObject.toJSONString(dto));
        po.setCreateTime(LocalDateTime.now());
        po.setChatType(ChatEnums.ChatType.contact);
        save(po);
    }

    @Override
    public ContactHistoryMessagePO save(BaseSaveDTO saveDTO) {
        return null;
    }
}
